// @ts-nocheck

import { cloneDeep } from "@pureadmin/utils";
import { BasicFormBox } from "../basic-form-box";
import { BasicTaleBox } from "../basic-table-box";
import { BasicTree } from "../basic-tree-box";
import { Workflow } from "./type";
type WT<P> = Workflow<P, keyof P>;

/**
 * 更新数据缓存
 * @param workflow 组件信息
 * @param workflowNewData 大缓存对象
 * @param workflowDefaultData 原始缓存对象 (reset)
 * @param isInit (是否需要更新原始缓存对象)
 */
export function updateWorkflowNewData<P>(
	workflow: WT<P>,
	workflowNewData: Record<keyof P, any>,
	workflowDefaultData?: Record<keyof P, any>,
	isInit: boolean = false
) {
	const option = workflow.option;
	const key = workflow.key;
	if (option instanceof BasicFormBox) {
		workflowNewData[key] = option.getFormValue();
	} else if (option instanceof BasicTaleBox) {
		workflowNewData[key] = option.getTableSource();
	} else if (option instanceof BasicTree) {
		workflowNewData[key] = option.getTreeSelection();
	}

	if (isInit) {
		workflowDefaultData = cloneDeep(workflowNewData);
	}
}
/**
 * 设置组件数据
 * @param workflow 目标组件
 * @param data 数据
 */
export function updateComponentInfo<P>(workflow: WT<P>, data: any) {
	const option = workflow.option;
	if (option instanceof BasicFormBox) {
		option.setFormValue(data);
	} else if (option instanceof BasicTaleBox) {
		option.setTableSource(data);
	} else if (option instanceof BasicTree) {
		option.setTreeSelection(data);
	}
}

/**
 * 设置默认值
 * @param workflow
 * @returns 完整对象
 */
export function setWorkflowDefaultOption<P>(workflow: WT<P>) {
	return {
		optionData: {},
		workflowSortNum: 1,
		...workflow,
		workflowState: {
			allowNextStep: true,
			allowPrevStep: true,
			disabled: false,
			...(workflow.workflowState || {}),
		},
	};
}
